package com.codeckz.libface

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.codeckz.libface.databinding.ActivityMainBinding
import com.codeckz.library_face.FaceGetOptions
import com.codeckz.library_face.FaceGetterPlugins
import com.codeckz.library_face.controller.ICameraController
import com.codeckz.library_face.ui.BaseActivity
import com.codeckz.library_face.ui.FaceGetterActivity
import com.codeckz.library_face.utils.ScreenUtils
import java.lang.Exception
import java.lang.reflect.Method


class MainActivity : BaseActivity<ActivityMainBinding>() {

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_main
    }

    private fun requestPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (context.checkSelfPermission(Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED
            ) {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.CAMERA),
                    100
                )
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        if (Build.VERSION.SDK_INT >= 28) {
            val lp = window.attributes
            lp.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            window.attributes = lp
        }
        super.onCreate(savedInstanceState)
        requestPermission()
        binding.btnOpen.setOnClickListener {
//            startActivityForResult(Intent(context, FaceGetterActivity::class.java), 200)
            val options = FaceGetOptions()
            options.isUVC = true
            options.checkNum = 1
            FaceGetterPlugins.startFace(context,{data ->
                val op = BitmapFactory.Options()
                op.inPreferredConfig = Bitmap.Config.RGB_565
                val bitmap = BitmapFactory.decodeByteArray(
                    data, 0,
                    data.size)
                binding.ivImage.setImageBitmap(bitmap)
            },options)
        }

        val width = ScreenUtils.getScreenWidth(context)
        val heighr = ScreenUtils.getScreenHeight(context)
        binding.tvSize.text = "${width}x${heighr}"

    }

}